﻿using System;
using System.Text.RegularExpressions;
using Gonte.Validation;

namespace Gonte.ObjectModel
{
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
    public class RegexFieldAttribute
        : FieldAttribute
    {
        public RegexFieldAttribute(string pattern, RegexOptions options)
        {
            ExceptionThrower.ThrowWhenNull("pattern", pattern);
            
            pattern_ = pattern;
            options_ = options;
        }

        public RegexFieldAttribute(string pattern)
        {
            ExceptionThrower.ThrowWhenNull("pattern", pattern);

            pattern_ = pattern;
        }

        /// <summary>
        /// Regular expression the field is supposed to match 
        /// </summary>
        public string Pattern
        {
            get { return pattern_; }
        }

        public override FieldValidator CreateValidator()
        {
            return new FieldRegexValidator(pattern_, options_);
        }

        //public override BaseValidator CreateWebValidationControl()
        //{
        //    RegularExpressionValidator validator = new RegularExpressionValidator();
        //    validator.ValidationExpression = pattern_;

        //    if (!string.IsNullOrEmpty(ErrorMessage))
        //    {
        //        validator.ErrorMessage = ErrorMessage;
        //    }

        //    return validator;
        //}

        private readonly string pattern_;
        private readonly RegexOptions options_;
    }
}
